package helper

import (
	"net"
	"strings"
)

func GetExternal() (string, error) {
	conn, err := net.Dial("udp", "www.baidu.com:80")
	if err != nil {
		return "", err
	}
	defer conn.Close()
	return strings.Split(conn.LocalAddr().String(),":")[0], nil
}

func GetInternal() (string, error) {
	intraIp := ""
	addrs, err := net.InterfaceAddrs()
	if err != nil {
		return "", err
	}
	for _, a := range addrs {
		if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
			if ipnet.IP.To4() != nil {
				//fmt.Println(ipnet.IP.String() + "\n")
				intraIp = ipnet.IP.String()
				break
			}
		}
	}
	return intraIp, nil
}
